package umacis.core;

import umacis.data.Bindings;
import umacis.data.SystemKeys;
import umacis.data.description.ViewDescription;
import umacis.data.elements.Action;
import umacis.data.elements.Binding;
import umacis.data.elements.Key;

import com.sun.lwuit.Form;
import com.sun.lwuit.KeyEvent;
import com.sun.lwuit.KeyEvent.KeyListener;
import com.sun.lwuit.util.Log;

public class EventListener implements KeyListener {

	private int secretKeysIndex;
	private boolean isKeyHeld;
	private boolean isShiftDown;
	private Umacis umacis;
	
	public EventListener() {
		KeyEvent.getInstance().setListener(this);
		umacis = Umacis.getInstance();
	}

	int ignoreKeyCode  = Key.UNDEFINED; 
	
	
	
	public boolean keyPressed(Form sourceForm, int keyCode) {
		Log.p("keyPressed: "+SystemKeys.getInstance().getSystemKey(keyCode).name, Log.DEBUG);
		if (secretKeysIndex == 0) {
			if (keyCode == Key.ONE) {
				secretKeysIndex++;
				Log.p("Secret code nr: " + secretKeysIndex,Log.DEBUG);
			}
		} else if (secretKeysIndex == 1) {
			if (keyCode == Key.THREE) {
				secretKeysIndex++;
				Log.p("Secret code nr: "  + secretKeysIndex,Log.DEBUG);
			} else {
				secretKeysIndex = 0;
				//Log.p(secretCode);
			}
		}
		return false; //XXXok?
    }

	//XXX ?replace with longKeyPressed(int keyCode) { }
    public boolean keyRepeated(Form sourceForm, int keyCode) { // is there a keybinding for this held key?
    	Log.p("keyRepeated: "+SystemKeys.getInstance().getSystemKey(keyCode).name, Log.DEBUG);
    	if (isKeyHeld) return true;
    	
		if (keyCode == ignoreKeyCode) { //XXX changed super.keyRepeated(keyCode); 
			return false;  } //only check keyCode the first time it repeats into this method
		else ignoreKeyCode = Key.UNDEFINED;	
    	int state = isShiftDown ? Key.SHIFTEDANDHELD : Key.HELD;
    	processKeyEvent(sourceForm, new int[] { state, keyCode } );
    	
    	//Action action = umacis.getAction(Binding.LOCAL, keyCode, state);
    	//action.execute();
//		if ( !action.isEmpty ) isKeyHeld = true; 
//		else {
//			action = umacis.getAction(Binding.GLOBAL, keyCode, state);
//			action.execute();
//			if ( !action.isEmpty ) isKeyHeld = true;
//			else { 
//				ignoreKeyCode = keyCode;
//			return false; //XXX changed super.keyRepeated(keyCode); 
//			
//			}
//					
//		}
		return true; //Do nothing is true
    }

    
    public boolean  keyReleased(Form sourceForm, int keyCode)  { 
    	Log.p("keyReleased: "+SystemKeys.getInstance().getSystemKey(keyCode).name, Log.DEBUG);
    	ignoreKeyCode = Key.UNDEFINED;	//for keyRepeat purposes
    	if (isKeyHeld){
    		isKeyHeld = false;
    		return true;//catch the final keyrelease of a held key that was acted on 
    	}
    	else  {
			if (secretKeysIndex == 2) {
				if (keyCode == Key.STAR) {
					Log.p("Secret code nr: " + secretKeysIndex, Log.DEBUG);
					showEditScreen();
					secretKeysIndex = 0;
					return true;
				}
			}
			secretKeysIndex = 0;
			int state = isShiftDown ? Key.SHIFTED : Key.NORMAL;
			processKeyEvent(sourceForm, new int[] { state, keyCode } );
			
//			Action action = umacis.getAction(Binding.LOCAL, keyCode, state);
//			action.execute();
//			if ( action.isEmpty  ) 	{
//				if (!umacis.executeGlobalAction(keyCode, state)){
//					emptyAction(keyCode, state, action); //no action found that executed
//				}
//						
//			}
			return 	false;	
    	}
    }
    
    
    private void processKeyEvent(Form sourceForm, int[] key) {
    	ViewDescription viewDescription = (ViewDescription)sourceForm.getClientProperty(ViewDescription.KEY);
    	//Bindings bindings = null;
    	if (viewDescription == null) {
    		Log.p("ERROR viewDescription should not be null!!!!! in processKeyEvent. ", Log.ERROR);
    		return;
    	} //the source form has to have clientproperty of VievDesctiption!!!
    	Bindings bindings = viewDescription.getBindings();
    	Action action = bindings.getAction(key);
		if (action == null) { 
			bindings = Umacis.getInstance().getBindings();
			action = bindings.getAction(key);
		}
		if (action == null) {
			Log.p("There is no binding for key: " + key[0] +SystemKeys.getInstance().getSystemKey(key[1]).name , Log.INFO);
			return;
		}
		ActionClassManager.getInstance().execute(action);
		//Action action = description.getBindings().getAction(new int[] {keyCode, state});
		//if (action == null) return false;
    	
    }
    
    private void showEditScreen() {
    	Log.p("showEditScreen", Log.DEBUG);
	}

//	
//    public void emptyAction(int key, int state, Object action) {
//    	Log.p("KeyCatcher.defaultAction: action not found in local nor in global bindings for this key and state", Log.DEBUG);
//		
//		
//	}

	
}
